<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="400px" @close='close'>
    <el-form ref="form" :model="record" label-width="70px" :rules="rules" label-position="left" style="width:350px;margin: 0 30px;">
      <!-- <el-form-item :label="$t('名称')" prop="name">
        <el-input v-emoji v-model="record.name" style="width:217px;" size="small"></el-input>
      </el-form-item> -->
      <el-form-item style="width:325px;" :label="$t('IP类型')" prop="type">
        <el-radio-group v-model="record.type">
          <el-radio class="ipv4" label="ipv4">IPv4</el-radio>
          <el-radio class="ipv6" label="ipv6">IPv6</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('地址')" prop="addr">
        <el-input v-emoji v-model="record.addr" ref="recordAddr" style="width:217px;" size="small"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { setDnsInfo } from '@/api/network/dnsHosts/dns'
import { inputClass } from '@/utils/input-class.js'
export default {
  name: 'DnsDialog',
  data () {
    // var validateName = (rule, value, callback) => {
    //   if (String(value).trim() === '') {
    //     callback(new Error(this.$t('不能为空')))
    //   } else {
    //     callback()
    //   }
    // }
    var validateIp = (rule, value, callback) => {
      let ipv4 = /^((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}$/
      let ipv6 = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (this.record.type === 'ipv4' && !ipv4.test(value)) {
        callback(new Error(this.$t('请输入正确的ipv4地址')))
      } else if (this.record.type === 'ipv6' && !ipv6.test(value)) {
        callback(new Error(this.$t('请输入正确的ipv6地址')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        // id: null,
        type: 'ipv4',
        addr: ''
      },
      recordCopy: {
        // id: null,
        type: 'ipv4',
        addr: ''
      },
      rules: {
        // name: [{ required: false, validator: validateName, trigger: 'blur' }],
        addr: [{ required: false, validator: validateIp, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['recordAddr']
        inputClass(this.$refs, data)
      })
      if (data) {
        this.record = Object.assign(this.record, data)
      }
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          setDnsInfo(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('添加成功'),
                type: 'success'
              })
              this.close()
              this.$parent.listData()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    }
  }
}
</script>
